package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.TbSeller;
import com.dongdongshop.admin.model.TbSellerExample;
import com.dongdongshop.admin.vo.SellerVO;

import com.dongdongshop.mapper.TbSellerMapper;
import com.dongdongshop.service.SellerService;
import com.dongdongshop.shiro.ShiroUtils;
import com.dongdongshop.util.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SellerServiceImpl implements SellerService {
    @Autowired
    private TbSellerMapper sellerMapper;

    @Override
    public SellerVO getSellerByName(String sellerId) {
        TbSeller tbSeller = sellerMapper.selectByPrimaryKey(sellerId);
        SellerVO vo = new SellerVO();
        BeanUtils.copyProperties(tbSeller, vo);
        return vo;
    }

    @Override
    public void addSeller(SellerVO sellerVO) {
        //创建对象
        TbSeller tbSeller = new TbSeller();
        //将数据传输到容器中
        BeanUtils.copyProperties(sellerVO, tbSeller);
        //创建盐
        String salt = ShiroUtils.generateSalt(6);
        tbSeller.setSalt(salt);
        //创建加密算法
        String md5 = ShiroUtils.encryptPassword("MD5", tbSeller.getPassword(), tbSeller.getSalt(),10);
        tbSeller.setPassword(md5);
        sellerMapper.insertSelective(tbSeller);
    }

    @Override
    public List<TbSeller> getAll(Object o) {
        return sellerMapper.selectByExample(null);
    }

    @Override
    public List<SellerVO> getAllSeller(String name, String nickName) {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria = example.createCriteria();
        criteria.andStatusNotEqualTo("1");
        if (StringUtils.isNotBlank(name)) {
            criteria.andNameLike("%"+name+"%");
        }
        if (StringUtils.isNotBlank(nickName)) {
            criteria.andNickNameLike("%"+nickName+"%");
        }
        List<TbSeller> tbSellers = sellerMapper.selectByExample(example);
        List<SellerVO> collect = tbSellers.stream().map(v -> {
            SellerVO vo = new SellerVO();
                BeanUtils.copyProperties(v, vo);

            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void updateStauts(SellerVO sellerVO) {
        TbSellerExample example = new TbSellerExample();
        TbSellerExample.Criteria criteria = example.createCriteria();
        criteria.andSellerIdEqualTo(sellerVO.getSellerId());
        List<TbSeller> tbSellers = sellerMapper.selectByExample(example);
        TbSeller tbSeller1 = tbSellers.get(0);
        tbSeller1.setStatus(sellerVO.getStatus());
        sellerMapper.updateByPrimaryKeySelective(tbSeller1);
    }
}
